#include <stdio.h>
#include "sysConfig.h"

#define myConf_NPLATE 8
#define myConf_N_NETWORK 2
#define myConf_NPAIR 4
#define myConf_TEMPLATE 2

extern int N_PLATE;
extern int N_NETWORK;
extern int N_PAIR;
extern int N_TEMPLATE;
extern int PlateConf[MAX_PLATE_TOTAL][3];
extern int PairConf[MAX_PAIR_TOTAL][2];
extern int TemplateConf[MAX_PLATE_TOTAL][MAX_TEMPLATE];

int testPlateList(T_plateList PL){
    int i;
    int success = 1;
	
    T_plateList expected;
    expected.plate[0].absis = 1;    expected.plate[0].ordinat = 1;
    expected.plate[1].absis = 1;    expected.plate[1].ordinat = 2;
    expected.plate[2].absis = 2;    expected.plate[2].ordinat = 1;
    expected.plate[3].absis = 2;    expected.plate[3].ordinat = 2;
    expected.plate[4].absis = 1;    expected.plate[4].ordinat = 1;
    expected.plate[5].absis = 1;    expected.plate[5].ordinat = 2;
    expected.plate[6].absis = 2;    expected.plate[6].ordinat = 1;
    expected.plate[7].absis = 2;    expected.plate[7].ordinat = 2;
    
    
	// //Init PlateList
	for (i=0; i<N_PLATE; ++i){
		if (getAbs(PL, i) != getAbs(expected, i)) {
            success = 0;
        }
		if (getOrd(PL, i) != getOrd(expected, i)) {
            success = 0;
        }
    }
    
    return success;    

}


int testPlateMap(T_plateMap PM){
    int i,j;
    int success = 1;
    
    T_plateMap expected;
    
    expected.netw[0].nPlate = 4;
    expected.netw[0].IDList[0] = 0;
    expected.netw[0].IDList[1] = 1;
    expected.netw[0].IDList[2] = 2;
    expected.netw[0].IDList[3] = 3;
    expected.netw[1].nPlate = 4;
    expected.netw[1].IDList[0] = 4;
    expected.netw[1].IDList[1] = 5;
    expected.netw[1].IDList[2] = 6;
    expected.netw[1].IDList[3] = 7;
    
	for (i=0; i<N_NETWORK; ++i){
		if (getNPlateinNetw(PM,i) != getNPlateinNetw(expected,i)) {
            success = 0;
        } else {
            for (j=0; j < getNPlateinNetw(PM,i); ++j){
                //printf("  :: got %d expected %d\n",getIDnInNetw(PN, j) ,getIDnInNetw(PNexpected, j) );
                if (getIDnInNetw(PM, i,j) != getIDnInNetw(expected, i,j)) {
                    success = 0;
                }
            }
        }
    }
    
    return success;    
}

int testPairList(T_pairList PL){
    int i,j;
    int success = 1;
    
    T_pairList expected;
    
    expected.pair[0] = 4;  expected.pair[4] = 0;
    expected.pair[1] = 5;  expected.pair[5] = 1;
    expected.pair[2] = 6;  expected.pair[6] = 2;
    expected.pair[3] = 7;  expected.pair[7] = 3;

    for (i = 0; i < N_PLATE; ++i){
        if ( getPairID(PL, i) !=  getPairID(expected, i)) {
            success = 0;
        }
    }
    
    return success;    
}

int tesTemplateList(T_templateList TL){
    int i,j;
    int success = 1;
    
    T_templateList expected;
    
    expected.dt[0].temp[0] = 90;  expected.dt[0].temp[1] = 30;
    expected.dt[1].temp[0] = 30;  expected.dt[1].temp[1] = 90;
    expected.dt[2].temp[0] = 90;  expected.dt[2].temp[1] = 30;
    expected.dt[3].temp[0] = 30;  expected.dt[3].temp[1] = 90;
    expected.dt[4].temp[0] = 90;  expected.dt[4].temp[1] = 30;
    expected.dt[5].temp[0] = 30;  expected.dt[5].temp[1] = 90;
    expected.dt[6].temp[0] = 90;  expected.dt[6].temp[1] = 30;
    expected.dt[7].temp[0] = 30;  expected.dt[7].temp[1] = 90;
    

    for (i = 0; i < N_PLATE; ++i){
        for (j = 0; j < N_TEMPLATE; ++j) {
            if ( getTemplateID(TL, i, j)  != getTemplateID(expected, i, j) ){
                success = 0;
            }
        }
    }
    
    return success;    
}

int tesSetGetPlateDB(){
    int i,j;
    int success = 1;
    
    T_plateDB PDBx;
    initPlateDB (&PDBx);
    
    T_plateData temp;
    temp.condition = 1;
    temp.targetTemp = 0;
    temp.BGTemp = 0;
    temp.ActTemp = 0;
    
    // printf(":: %d\n", isPlateDataEqual(temp, temp));
    
    for ( i = 0; i < MAX_PLATE_TOTAL; ++i){
        if (!isPlateDataEqual(temp,getPlateData(PDBx,i))){
            // printf("%d\n",i);
            // printf("  condition %d \n  targetTemp %d \n  BGTemp %d \n  ActTemp %d\n\n",
            // getPlateData(PDBx,i).condition,
            // getPlateData(PDBx,i).targetTemp,
            // getPlateData(PDBx,i).BGTemp,
            // getPlateData(PDBx,i).ActTemp);
            
            success = 0;
        }
    }
    
    temp.condition = 0;
    temp.targetTemp = 23;
    temp.BGTemp = 45;
    temp.ActTemp = 123;
    
    setPlateData(&PDBx, 5, temp);
    if (!isPlateDataEqual(temp,getPlateData(PDBx,5))){
        // printf("set 5 get 5%d\n",i);
        success = 0;
    }
    if (isPlateDataEqual(temp,getPlateData(PDBx,0))){
        // printf("set 5 get 0\n");
        success = 0;
    }    
    temp.condition = 1;
    temp.targetTemp = 255;
    temp.BGTemp = 255;
    temp.ActTemp = 255;
    
    setPlateData(&PDBx, 4, temp);
    if (!isPlateDataEqual(temp,getPlateData(PDBx,4))){
        // printf("set 5 get 5%d\n",i);
        success = 0;
    }
    if (isPlateDataEqual(temp,getPlateData(PDBx,5))){
        // printf("set 5 get 0\n");
        success = 0;
    }
    return success;    
}

int main(){
    initDataSet();
	int i,j;
	
	printf("\n\nTEST START\n\n");
	T_plateList PlateList;
	T_plateMap PlateMap; 
	T_pairList PairList;
	T_templateList TemplateList;
	
	initPlateList(&PlateList);
    i = testPlateList(PlateList);
    printf("testPlateList : %s\n",(i)?"OK":"FAIL");
    
    initPlateMap(&PlateMap);
    i = testPlateMap(PlateMap);
    printf("testPlateMap : %s\n",(i)?"OK":"FAIL");
    
    initPairList(&PairList);
	i = testPairList(PairList);
    printf("testPairList : %s\n",(i)?"OK":"FAIL");
    
    
	initTemplateList(&TemplateList);
	i = tesTemplateList(TemplateList);
    printf("tesTemplateList : %s\n",(i)?"OK":"FAIL");

    i = tesSetGetPlateDB();
    printf("tesSetGetPlateDB : %s\n",(i)?"OK":"FAIL");
	
	printf("\n\nTEST END\n");
	return 0;
}

